package com.iwindplus.im.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.iwindplus.boot.domain.constant.CommonConstant;
import com.iwindplus.boot.domain.constant.CommonConstant.UserConstant;
import com.iwindplus.im.domain.dto.MsgIntegrationDetailDTO;
import com.iwindplus.im.domain.enums.CommandEnum;
import com.iwindplus.im.domain.enums.MsgStatusEnum;
import com.iwindplus.im.domain.enums.SendStatusEnum;
import com.iwindplus.im.domain.vo.DirectMsgVO;
import com.iwindplus.im.domain.vo.MsgIntegrationVO;
import com.iwindplus.im.domain.vo.SysNoticeMsgVO;
import com.iwindplus.im.server.model.DirectMsgDO;
import com.iwindplus.im.server.model.SysNoticeMsgDO;
import com.iwindplus.im.server.service.DirectMsgService;
import com.iwindplus.im.server.service.MsgIntegrationService;
import com.iwindplus.im.server.service.SysNoticeMsgService;
import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

/**
 * 消息集成业务层接口实现类.
 *
 * @author zengdegui
 * @since 2024/03/23 15:15
 */
@Slf4j
@Service
public class MsgIntegrationServiceImpl implements MsgIntegrationService {
    @Resource
    private DirectMsgService directMsgService;

    @Resource
    private SysNoticeMsgService sysNoticeMsgService;

    @Override
    public MsgIntegrationVO getMsg(MsgIntegrationDetailDTO entity) {
        Integer size = entity.getSize();
        String id = entity.getId();
        CommandEnum command = entity.getCommand();
        MsgIntegrationVO result = new MsgIntegrationVO();
        if (Objects.nonNull(command) && CharSequenceUtil.isNotBlank(id)) {
            if (CommandEnum.DIRECT_MSG.equals(command)) {
                // 未读消息
                DirectMsgDO directMsg = this.directMsgService.getById(id);
                if (Objects.nonNull(directMsg)) {
                    final DirectMsgVO vo = BeanUtil.copyProperties(directMsg, DirectMsgVO.class);
                    if (MsgStatusEnum.READ.equals(directMsg.getMsgStatus())) {
                        result.setReadMsgs(Arrays.asList(vo));
                    } else {
                        result.setUnReadMsgs(Arrays.asList(vo));
                    }
                }
            }
            if (CommandEnum.SYS_NOTICE_MSG.equals(command)) {
                // 未读消息
                SysNoticeMsgDO sysNoticeMsg = this.sysNoticeMsgService.getById(id);
                if (Objects.nonNull(sysNoticeMsg)) {
                    final SysNoticeMsgVO vo = BeanUtil.copyProperties(sysNoticeMsg, SysNoticeMsgVO.class);
                    result.setSysNoticeMsgs(Arrays.asList(vo));
                }
            }
        } else {
            final Integer limit = Optional.ofNullable(size).orElse(CommonConstant.NumberConstant.NUMBER_TEN);
            Long userId = entity.getCurrentUserId();
            Long orgId = entity.getOrgId();
            Criteria criteria = Criteria.where(UserConstant.ORG_ID).is(orgId)
                .and("sendStatus").is(SendStatusEnum.SUCCESS);
            // 未读消息
            final Query unReadMsgQuery = Query.query(criteria)
                .addCriteria(Criteria.where("receiverId").is(userId).and("msgStatus").is(MsgStatusEnum.UN_READ))
                .limit(limit).with(Sort.by(Sort.Order.desc("sendTime")));
            List<DirectMsgDO> unReadMsgs = this.directMsgService.list(unReadMsgQuery);
            if (CollUtil.isNotEmpty(unReadMsgs)) {
                List<DirectMsgVO> voList = BeanUtil.copyToList(unReadMsgs, DirectMsgVO.class);
                result.setUnReadMsgs(voList);
                result.setUnReadMsgCount(voList.size());
            }
            // 已读消息
            final Query readMsgQuery = Query.query(criteria)
                .addCriteria(Criteria.where("receiverId").is(userId).and("msgStatus").is(MsgStatusEnum.READ))
                .limit(limit).with(Sort.by(Sort.Order.desc("sendTime")));
            List<DirectMsgDO> readMsgs = this.directMsgService.list(readMsgQuery);
            if (CollUtil.isNotEmpty(readMsgs)) {
                List<DirectMsgVO> voList = BeanUtil.copyToList(readMsgs, DirectMsgVO.class);
                result.setReadMsgs(voList);
                result.setReadMsgCount(voList.size());
            }
            // 系统通知消息
            final Query sysNoticeMsgQuery = Query.query(criteria)
                .limit(limit).with(Sort.by(Sort.Order.desc("sendTime")));
            List<SysNoticeMsgDO> sysNoticeMsgs = sysNoticeMsgService.list(sysNoticeMsgQuery);
            if (CollUtil.isNotEmpty(sysNoticeMsgs)) {
                List<SysNoticeMsgVO> voList = BeanUtil.copyToList(sysNoticeMsgs, SysNoticeMsgVO.class);
                result.setSysNoticeMsgs(voList);
                result.setSysNoticeMsgCount(voList.size());
            }
            // 已回收
            final Query recycleMsgQuery = Query.query(criteria)
                .addCriteria(Criteria.where("receiverId").is(userId).and("msgStatus").is(MsgStatusEnum.RECYCLED))
                .limit(limit).with(Sort.by(Sort.Order.desc("sendTime")));
            List<DirectMsgDO> recycleMsgs = this.directMsgService.list(recycleMsgQuery);
            if (CollUtil.isNotEmpty(recycleMsgs)) {
                List<DirectMsgVO> voList = BeanUtil.copyToList(recycleMsgs, DirectMsgVO.class);
                result.setRecycleMsgs(voList);
                result.setRecycleMsgCount(voList.size());
            }
        }
        return result;
    }
}
